﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MenuControl;
using Zeus.Classes.Documents;
using System.IO;
using Zeus.Classes;
using System.Xml.Linq;
using Zeus.Classes.Views;
using Zeus.UserControls;
using Zeus.Classes.Events;
using Zeus.Events;

namespace Zeus
{
    public partial class MainPage : UserControl
    {
        int counter_ToolBox = 0;
        int counter_SoultionExplorer = 0;
        int counter_TeamExplorer = 0;
        int counter_ServerExplorer = 0;
        int counter_Proparties = 0;
        int counter_Debug = 0;

        public MainPage()
        {
            
            InitializeComponent();
            App.EventAggegator.GetEvent<PropertiesPanel>().Subscribe(PropertiesButton);
           
        }

        public void AddTolBox(int i, string name)
        {
            for (int j = 1; j <= i; j++)
            {
                ListBox listBox_1 = new ListBox();
                listBox_1.Width = 180;
                listBox_1.Height = 500;
                listBox_1.VerticalAlignment = VerticalAlignment.Center;
                listBox_1.Margin = new Thickness(20, 20, 0, 0);
                listBox_1.BorderThickness = new Thickness(10, 10, 10, 10);
                TextBlock textBlock = new TextBlock();
                textBlock.Text = name;
                textBlock.TextAlignment = TextAlignment.Center;
                textBlock.FontSize = 24;
                SolidColorBrush s = new SolidColorBrush();
                s.Color = Colors.Red;
                textBlock.Foreground = s;
                listBox_1.Items.Add(textBlock.Text);
                LayoutRoot.Children.Add(listBox_1);

            }
        }

        private void MenuItem_Click_New(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Alert("you pressed: " + (sender as MenuItem).MenuText);
        }

        private void MenuItem_Click_Open(object sender, RoutedEventArgs e)
        {
            ShowOpenFileDialog();
        }

        private void MenuItem_Click_Save(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Alert("you pressed: " + (sender as MenuItem).MenuText);
        }

        private void MenuItem_Click_SaveAs(object sender, RoutedEventArgs e)
        {
            ShowSaveFileDialog();
        }

        private void ShowOpenFileDialog()
        {
            OpenFileDialog OpenDlg = new OpenFileDialog();
            OpenDlg.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";
            OpenDlg.FilterIndex = 2;
            OpenDlg.ShowDialog();
        }

        private void ShowSaveFileDialog()
        {
            SaveFileDialog SaveDlg = new SaveFileDialog();
            SaveDlg.ShowDialog();
        }

        private void MenuItem_click_Import_Sound(object sender, RoutedEventArgs e)
        {   
            OpenFileDialog ofd = new OpenFileDialog();
            if ((bool)ofd.ShowDialog() == true)
            {
                
                Stream Stream_sound = ofd.File.OpenRead();
                SoundDocument SoundDocument_1 = new SoundDocument(Stream_sound);
                soundView SoundView_1 = new soundView(SoundDocument_1);
                SoundDocument_1.Lview.Add(SoundView_1);
                SoundView_1.Show();
            }
        }

        private void MenuItem_Click_OpenImage(object sender, RoutedEventArgs e)
        {
            OpenFileDialog OpenImage = new OpenFileDialog();
            OpenImage.Filter = "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg | All Files (*.*)|*.*";
            OpenImage.ShowDialog();
            Stream Stream = OpenImage.File.OpenRead();      //it crached if i closed dialog without chooseing image

            ImageDocument IDoc = new ImageDocument(Stream);
            DisplayImageView IView = new DisplayImageView(IDoc);
            IDoc.Lview.Add(IView);

            IView.Show();
        }

        private void MenuItem_Click_openvideo(object sender, RoutedEventArgs e)
        {
            OpenFileDialog vdialog = new OpenFileDialog();
            vdialog.Filter = "video file(.wmv)|*.wmv";
            if ((bool)vdialog.ShowDialog())
            {
             
                Stream st=vdialog.File.OpenRead();
                videoDocument vdoc = new videoDocument(st);
                viewvideo Vview = new viewvideo(vdoc);
                Vview.Show();

            }
            else
            {
                MessageBox.Show("u didn't select a file");
            }
        }
       
       
        #region AddToolBar
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (counter_TeamExplorer == 0)
            {
                AddTolBox(Convert.ToInt32(Resource_file_1.count), Resource_file_1.textBlock_team_explorer);
                counter_TeamExplorer++;

            }
            else
            { MessageBox.Show("There are Team Explorer  already opened"); }
            
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            if (counter_ToolBox == 0)
            {
                AddTolBox(Convert.ToInt32(Resource_file_1.count), Resource_file_1.textBlock_tool_Box);
                counter_ToolBox++;
            }
            else { MessageBox.Show("there are Tool Box already opened"); }
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            if (counter_SoultionExplorer == 0)
            {
                AddTolBox(Convert.ToInt32(Resource_file_1.count), Resource_file_1.textblock_soultion);
                counter_SoultionExplorer++;
            }
            else { MessageBox.Show("there ara Solution Explorer already opened"); }
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            if (counter_ServerExplorer == 0)
            {
                AddTolBox(Convert.ToInt32(Resource_file_1.count), Resource_file_1.textblock_server_explorer);
                counter_ServerExplorer++;
            }
            else { MessageBox.Show("there are server explorer already opened"); }
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            if (counter_Debug == 0)
            {
                AddTolBox(Convert.ToInt32(Resource_file_1.count), Resource_file_1.textBlock_Debug);
                counter_Debug++;
            }
            else { MessageBox.Show("there are Debug menu opened"); }
        }

        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            if (counter_Proparties == 0)
            {
                AddTolBox(Convert.ToInt32(Resource_file_1.count), Resource_file_1.textBlock_proparties);
                counter_Proparties++;
            }
            else { MessageBox.Show("there are prparties menu already opened"); }
        }
        #endregion

        private void MenuItem_Click_6(object sender, RoutedEventArgs e)
        {
            List<XElement> list = NewProject();
            NewProjectChildWindow n = new NewProjectChildWindow(list);
            n.Show();
        }

        private List<XElement> NewProject()
        {
            XDocument doc = XDocument.Load("ProjectTypes.xml");
            var ProjectTypes =
            (from project in doc.Descendants() where project.Name == "type" select project).ToList();
            return ProjectTypes;
        }

        private void MenuItem_Click_OpenProject(object sender, RoutedEventArgs e)
        {
            OpenProjectChildWindow Window = new OpenProjectChildWindow();
            Window.Show();
        }

        public void PropertiesButton(object obj)
        {
            ControlsProperties CP = new ControlsProperties();
            CP.Content( ZeusTitle , Prop.Prop1.BConetnt);
            CP.Size(ZeusTitle, Prop.Prop1.BWidth, Prop.Prop1.BHeight);
            CP.Opacity(ZeusTitle, Prop.Prop1.BOpacity);
            CP.Margin(ZeusTitle, Prop.Prop1.BMarginLeft, Prop.Prop1.BMarginTop, Prop.Prop1.BMarginRight, Prop.Prop1.BMarginButtom);
            CP.Name(ZeusTitle, Prop.Prop1.BName);
            CP.GenerateCode(CP.Data , ZeusTitle);
            
        }
    }
}